(define props (java.util.Properties))
(define session (javax.mail.Session:getDefaultInstance props #!null))

(define my-service::com.google.gdata.client.contacts.ContactsService (com.google.gdata.client.contacts.ContactsService "jiffy-contacts-service-1"))
(my-service:setUserCredentials "andre@ammunerve.com" "x")

(define (dump-groups)
  (letrec ((group-feed-url (java.net.URL "https://www.google.com/m8/feeds/groups/default/full"))
           (group-result-feed::com.google.gdata.data.contacts.ContactGroupFeed (my-service:getFeed group-feed-url <com.google.gdata.data.contacts.ContactGroupFeed>))
           (next (lambda (cnt)
                   (if (< cnt ((group-result-feed:getEntries):size))
                     (begin
                       (display ((((group-result-feed:getEntries):get cnt):getTitle):getPlainText))
                       (newline)
                       (next (+ cnt 1)))))))
    (next 0)))
(define (get-group-id group-name)
  (letrec ((group-feed-url (java.net.URL "https://www.google.com/m8/feeds/groups/default/full"))
           (group-result-feed::com.google.gdata.data.contacts.ContactGroupFeed (my-service:getFeed group-feed-url <com.google.gdata.data.contacts.ContactGroupFeed>))
           (next (lambda (cnt)
                   (if (< cnt ((group-result-feed:getEntries):size))
                     (if (equal?
                           group-name
                           ((((group-result-feed:getEntries):get cnt):getTitle):getPlainText))
                       (((group-result-feed:getEntries):get cnt):getId)
                       (next (+ cnt 1)))))))
    (next 0)))
(define (get-group-name group-id)
  (letrec ((group-feed-url (java.net.URL "https://www.google.com/m8/feeds/groups/default/full"))
           (group-result-feed::com.google.gdata.data.contacts.ContactGroupFeed (my-service:getFeed group-feed-url <com.google.gdata.data.contacts.ContactGroupFeed>))
           (next (lambda (cnt)
                   (if (< cnt ((group-result-feed:getEntries):size))
                     (if (equal?
                           group-id
                           (((group-result-feed:getEntries):get cnt):getId))
                       ((((group-result-feed:getEntries):get cnt):getTitle):getPlainText)
                       (next (+ cnt 1)))))))
    (next 0)))

(define feed-url (java.net.URL "https://www.google.com/m8/feeds/contacts/default/full"))

(define my-query::com.google.gdata.client.contacts.ContactQuery (com.google.gdata.client.contacts.ContactQuery feed-url))
(my-query:setMaxResults 1000)
(my-query:setGroup (get-group-id "wedding"))
(define result-feed::com.google.gdata.data.contacts.ContactFeed (my-service:query my-query <com.google.gdata.data.contacts.ContactFeed>))

(define (dump-email-addresses email-addresses)
  (letrec ((next (lambda (cnt)
                   (if (>= cnt 0)
                     (let ((email-address (email-addreses:get cnt)))
                       (display (email-address:getAddress))
                       (newline)
                       (next (- cnt 1)))))))
    (next (- (email-addresses:size) 1))))
(define (get-full-name name)
 (if (name:hasFullName)
    ((name:getFullName):getValue)
    "no full name"))
(define (get-email-addresses email-addresses)
  (if (= (email-addresses:size) 0)
    (list "111 missing!!!")
    (letrec ((next (lambda (l cnt)
                     (if (< cnt (email-addresses:size))
                       (next 
                         (append l (list ((email-addresses:get cnt):getAddress)))
                         (+ cnt 1))
                       l))))
      (next (list) 0))))
(define (get-groups groups)
  (letrec ((next (lambda (l cnt)
                   (if (< cnt (groups:size))
                     (next 
                       (append l (list (get-group-name ((groups:get cnt):getHref))))
                       (+ cnt 1))
                     l))))
    (next (list) 0)))
(define (dump-entries result-feed)
  (letrec ((next (lambda (cnt)
                   (if (< cnt ((result-feed:getEntries):size))
                     (let ((entry ((result-feed:getEntries):get cnt)))
                       (if (entry:hasName)
                         (let ((name (entry:getName)))
                           (display (get-full-name name))
                           (for-each (lambda (x) (display "\t") (display x)) (get-email-addresses (entry:getEmailAddresses)))
                           (newline)))
                       (next (+ cnt 1)))))))
    (next 0)))
(with-output-to-file "wedding.txt" (lambda () (dump-entries result-feed)))

